package com.wolf.cx330.view.view;

import android.content.Context;
import android.graphics.Camera;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.Nullable;

import com.wolf.cx330.base.BaseKt;

public class CameraView extends View {
    Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    int IMAGE_WIDTH = 300;
    Camera camera = new Camera();

    public CameraView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        camera.rotateX(30F);
        camera.setLocation(0, 0, -16);
        //
        canvas.save();
        canvas.translate(getWidth() / 2F, getHeight() / 2F);
        canvas.rotate(-30);
        camera.applyToCanvas(canvas);
        canvas.clipRect( -IMAGE_WIDTH, 0, IMAGE_WIDTH, IMAGE_WIDTH);
        canvas.rotate(30);
        canvas.translate(-getWidth() / 2F, -getHeight() / 2F);
        canvas.drawBitmap(BaseKt.getAvatar(getResources(), IMAGE_WIDTH), getWidth() / 2F - IMAGE_WIDTH / 2F, getHeight() / 2F - IMAGE_WIDTH / 2F, paint);
        canvas.restore();
        canvas.save();
        //  5.移动回去
        canvas.translate(getWidth() / 2F, getHeight() / 2F);
        //  4. 切完转回去
        canvas.rotate(-30);
        //  3.再切
        canvas.clipRect( -IMAGE_WIDTH, -IMAGE_WIDTH, IMAGE_WIDTH, 0);
        //  2.再旋转
        canvas.rotate(30);
        //  1.先移动
        canvas.translate(-getWidth() / 2F, -getHeight() / 2F);
        canvas.drawBitmap(BaseKt.getAvatar(getResources(), IMAGE_WIDTH), getWidth() / 2F - IMAGE_WIDTH / 2F, getHeight() / 2F - IMAGE_WIDTH / 2F, paint);
        canvas.restore();


    }
}
